package com.ccyang;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;

/**
 * @author: yangjinpeng
 * @date: 2018-12-23
 * @description: 收集器
 */
public class CollectDemo {

    private static class Student {
        private String name;
        private int age;
        private Gender gender;
        private Garde garde;

        public Student(String name, int age, Gender gender, Garde garde){
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.garde = garde;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Gender getGender() {
            return gender;
        }

        public void setGender(Gender gender) {
            this.gender = gender;
        }

        public Garde getGarde() {
            return garde;
        }

        public void setGarde(Garde garde) {
            this.garde = garde;
        }

        @Override
        public String toString() {
            return "[name=" + name + ", age=" + age + ", gender=" + gender + ", garde=" + garde + "]";
        }
    }
    /**
     * 性别
     */
    private enum Gender{
        MALE, FEMALE
    }

    /**
     * 班级
     */
    private enum Garde{
        ONE,
        TWO,
        THREE,
        FOUR
    }

    public static void main(String[] args) {

        // 测试数据
        List<Student> studentList = Arrays.asList(
                new Student("小明", 12, Gender.MALE, Garde.ONE),
                new Student("小李", 13, Gender.FEMALE, Garde.THREE),
                new Student("小王", 14, Gender.MALE, Garde.TWO),
                new Student("小宋", 11, Gender.FEMALE, Garde.FOUR),
                new Student("小张", 12, Gender.MALE, Garde.ONE),
                new Student("小样", 13, Gender.MALE, Garde.FOUR),
                new Student("小龙", 12, Gender.FEMALE, Garde.ONE),
                new Student("小虎", 15, Gender.FEMALE, Garde.TWO),
                new Student("小周", 13, Gender.MALE, Garde.TWO),
                new Student("小成", 12, Gender.FEMALE, Garde.THREE),
                new Student("小满", 10, Gender.MALE, Garde.TWO),
                new Student("小美", 11, Gender.FEMALE, Garde.FOUR)
        );

        // 得到所有学生年龄列表
        // s -> s.getAge()  -->  Student::getAge ,这样不会多生成一个类似 lambda$0 这样的函数
        List<Integer> collect = studentList.stream().map(Student::getAge).collect(Collectors.toList());
        System.out.println("所有学生年龄：" + collect);
        // set集合可以去重
        Set<Integer> collect2 = studentList.stream().map(Student::getAge).collect(Collectors.toCollection(TreeSet::new));
        System.out.println("所有学生年龄：" + collect2);

        // 统计汇总信息
        IntSummaryStatistics agesIntSummaryStatistics = studentList.stream().collect(Collectors.summarizingInt(Student::getAge));
        System.out.println("学生汇总信息：" + agesIntSummaryStatistics);

        // 分块(相当于两个组的分组)
        Map<Boolean, List<Student>> genders =
                studentList.stream().collect(Collectors.partitioningBy(s -> s.getGender() == Gender.MALE));
//        System.out.println("男女学生列表：" + genders);
        MapUtils.verbosePrint(System.out, "男女学生列表", genders);

        // 分组
        Map<Garde, List<Student>> grades = studentList.stream().collect(Collectors.groupingBy(Student::getGarde));
        MapUtils.verbosePrint(System.out, "班级学生列表", grades);

        // 分组后获取组的信息
        Map<Garde, Long> gradesCount = studentList.stream().collect(Collectors.groupingBy(Student::getGarde,
                Collectors.counting()));
        MapUtils.verbosePrint(System.out, "班级学生人数列表", gradesCount);

    }

}
